import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';
import 'package:pipedream_lighting/src/home/view_model.dart';

import '../tool/assets.dart';

class CustomRotationBtn extends RotateButton {
  CustomRotationBtn(
      {required super.child,
      super.startAngle = 3 * pi / 4,
      super.endAngle = pi / 4,
      super.clockwise = true,
      required super.max,
      required super.min,
      required super.value,
      required super.unit,
      required super.snapping,
      required super.onChange,
      required super.onChangeEnd});
  @override
  RotateButtonState<RotateButton> createState() => _CustomRotationBtnState();
}

class _CustomRotationBtnState extends RotateButtonState<RotateButton> {
  @override
  double snappinAngle(double angle, int degrees) {
    if (degrees >= 45 && degrees <= 90) {
      return pi / 2.0;
    } else if ((degrees < 45 || degrees > 315)) {
      return 0;
    } else if (degrees <= 315) {
      return 3 * pi / 2;
    } else {
      return angle;
    }
  }
}

class kRotationBtn extends StatefulWidget {
  final RotateModel data;

  /// 角度变化的回调函数
  final void Function(double angle, int degrees, dynamic changeValue) onChange;

  /// 拖动结束时的回调函数
  final void Function(double angle, int degrees, dynamic changeValue)?
      onChangeEnd;
  kRotationBtn(
      {super.key,
      required this.data,
      required this.onChange,
      this.onChangeEnd});

  @override
  State<kRotationBtn> createState() => _kRotationBtnState();
}

class _kRotationBtnState extends State<kRotationBtn> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, BoxConstraints constraints) {
      double w = constraints.maxHeight -
          widget.data.padding.top -
          widget.data.padding.bottom;
      double gap = 6.5 * GlobalConfig.scaleH;
      return Container(
        color: Colors.transparent,
        child: SizedBox(
          width: w,
          height: w,
          child: Stack(
            children: [
              ///旋钮的刻度背景
              Positioned(
                  left: 0,
                  right: 0,
                  top: 0,
                  child: Image.asset(widget.data.backImg)),

              ///旋钮的可触图片
              Positioned(
                left: gap,
                right: gap,
                bottom: gap,
                top: gap,
                child: CustomRotationBtn(
                    max: widget.data.max,
                    min: widget.data.min,
                    value: widget.data.value,
                    unit: widget.data.unit,
                    snapping: widget.data.snapping,
                    onChange: widget.onChange,
                    onChangeEnd: widget.onChangeEnd,
                    child: Image.asset(widget.data.tintImg)),
              ),

              ///旋钮上面的文字
              Positioned(
                left: gap,
                right: gap,
                bottom: gap,
                top: gap,
                child: IgnorePointer(
                  ///禁止手势感应
                  ignoring: true,

                  child: Center(
                      child: Text(
                    widget.data.title,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 14 * GlobalConfig.scaleH,
                        fontWeight: FontWeight.bold),
                  )),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }
}
